﻿using AutoMapper;
using MediatR;
using SCM.API.Write.Application.Command.Purchase;
using SCM.Domain;
using SCM.ErrorCode;
using SCM.Infrastructure.Implement;
using System.Security.Claims;

namespace SCM.API.Write.Application.Handler.Purchase
{
    public class AddWareHouseHandler : IRequestHandler<AddWareHouseCommand, APIResult<int>>
    {
        
        DatabaseContext db;

        public AddWareHouseHandler(DatabaseContext db)
        {
          
            this.db = db;
        }


        public Task<APIResult<int>> Handle(AddWareHouseCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var res = db.SqlSugar.Queryable<PurchaseManagementModel>().Where(x => x.Id == request.Id).First();
            if (res == null)
            {
                result.Code = APIEnum.Fail;
                result.Msg = "不存在";
                return Task.FromResult(result);
            }
            if(res.State==1)
            {
                result.Code = APIEnum.Fail;
                result.Msg = "产品已入库";
                return Task.FromResult(result);
            }
            res.LocationId = request.LocationId;
            res.Bacth=request.Bacth;
            res.State=1;
            int r = db.SqlSugar.Updateable(res).ExecuteCommand();
           
                result.Msg = "入库成功";
                result.Data = r;
                result.Code = APIEnum.Success;
           
            return Task.FromResult(result);
        }
    }
}
